package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Collar;
import com.zzld.modules.autoinn.entity.Coupon;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.service.ICollarService;
import com.zzld.modules.autoinn.service.IConponService;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.common.dto.CommonJSON;

@Controller
@RequestMapping("/weixin/f/autoinn/tui")
public class TuiController {

	private String tui = "/wx/autoinn/tui";
	private String dui = "/wx/autoinn/duiquan";

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IConponService couponService;

	@Autowired
	private ICollarService collarService;

	@RequestMapping(method = RequestMethod.GET)
	public String tui(Model model, HttpServletRequest request) {
		return tui;
	}

	@RequestMapping(value = "dui", method = RequestMethod.GET)
	public String dui(Model model, HttpServletRequest request) {
		String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session");
		 //String openid="123";
		 //String wxaccount ="12312321";
		try {

			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);

			// 获取该用户未过期优惠券

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("nowtime", new Date());
			map.put("wcaccount", wxaccount);
			map.put("openid", openid);
			List<Coupon> list1 = couponService.selectbyzhifu(map);
		
			model.addAttribute("coupon", list1.size() == 0 ? null : list1);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dui;
	}

	@RequestMapping(value = "ling", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON kq(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		String phone = request.getParameter("phone");
		if (phone != null) {
			 String openid = CookieUtils.getCookie(request, "autoinn_session");
			String wxaccount = CookieUtils.getCookie(request, "wx_session");
			//String openid = "234";
			//String wxaccount = "12312321";
			Customer customer1 = null;
			Customer customer2 = null;
			try {

				 openid = Base64Coder.decode(openid);
				wxaccount = Base64Coder.decode(wxaccount);
				customer1 = customerService.findCustomer(wxaccount, openid);// 当前用户
				customer2 = customerService.findCustomerByPhone(wxaccount,
						phone);// 推广用户
			} catch (Exception e) {
				json.setMessage(e.getMessage());
				json.setSuccess(false);
				e.printStackTrace();
			}

			Map<String, Object> map = new HashMap<String, Object>();

			// 先找出推广优惠券
			map.put("nowtime", new Date());
			map.put("couponZhi", 4);
			List<Coupon> list = couponService.selectbyjuan(map);

			List<Collar> co = null;
			if (!customer1.getPhoneNum().equals(phone)) {
				if (list.size() > 0) {
					// 推广人 获得一张优惠券
					try {
				       //查询是否已领取
						Example example = new Example(Collar.class);
						example.createCriteria().andEqualTo("customerId", customer2.getCustomerId())
						  .andEqualTo("phoneNum", customer1.getPhoneNum());
						co = collarService.selectByExample(example);
						if(co.size()==0){
							Collar c1 = new Collar();
							c1.setId(UuidUtil.uuid().toString());
							c1.setCustomerId(customer2.getCustomerId());
							c1.setCounponId(list.get(0).getCouponId());
							c1.setCollarTime(new Date());
							c1.setWxaccount(wxaccount);
							c1.setOpenid(customer2.getOpenid());
							c1.setPhoneNum(customer1.getPhoneNum());
							c1.setState(0);
							if(collarService.save(c1)>0){
								json.setMessage("恭喜您送推荐人一张"+list.get(0).getCouponName());
								json.setSuccess(true);
							}
						}else{
							json.setMessage("您已送过一张"+list.get(0).getCouponName()+",请勿重复送券!");
							json.setSuccess(false);
						}
					} catch (Exception e) {
						json.setMessage("出现异常!"+e.getMessage());
						json.setSuccess(false);
					}
				}
			} else {
				json.setMessage("请勿填写自己电话号码!");
				json.setSuccess(false);
			}
		}

		return json;
	}
}
